package com.dunshan.prcexp.util;

import com.dunshan.prcexp.base.listener.TestListener;
import com.dunshan.prcexp.response.content.ResultContent;
import lombok.extern.log4j.Log4j2;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author liwen406
 * @program: dunshan-pef
 * @description:
 * @date 2022-08-25 10:33:22
 */
@Log4j2
public class ThreadLocalUtil {
    private static ThreadLocal<AtomicInteger> caseNoThreadLocal = ThreadLocal.withInitial(AtomicInteger::new);
    private static ThreadLocal<AtomicInteger> stepNoThreadLocal = ThreadLocal.withInitial(AtomicInteger::new);
//    private static ThreadLocal<Man> manUrlThreadLocal = ThreadLocal.withInitial(Man::new);
//    private static ThreadLocal<Old> oldUrlThreadLocal = ThreadLocal.withInitial(Old::new);
//    private static ThreadLocal<Other> otherUrlThreadLocal = ThreadLocal.withInitial(Other::new);
    private static ThreadLocal<Integer> useDhfManThreadLocal = ThreadLocal.withInitial(() -> {
        return 1;
    });

    public ThreadLocalUtil() {
    }

    public static void incCaseNo() {
        ((AtomicInteger)caseNoThreadLocal.get()).incrementAndGet();
    }

    public static void incStepNo() {
        ((AtomicInteger)stepNoThreadLocal.get()).incrementAndGet();
    }

    public static int getCaseNo() {
        return ((AtomicInteger)caseNoThreadLocal.get()).intValue();
    }

    public static int getStepNo() {
        return ((AtomicInteger)stepNoThreadLocal.get()).intValue();
    }

    public static void clearCaseStep() {
        caseNoThreadLocal.remove();
        stepNoThreadLocal.remove();
    }

//    public static void setManUrl(Man manUrl) {
//        manUrlThreadLocal.set(manUrl);
//    }
//
//    public static Man getManUrl() {
//        return (Man)manUrlThreadLocal.get();
//    }
//
//    public static void setOtherUrl(Other otherUrl) {
//        otherUrlThreadLocal.set(otherUrl);
//    }
//
//    public static Other getOtherUrl() {
//        return (Other)otherUrlThreadLocal.get();
//    }
//
//    public static void setOldUrl(Old oldUrl) {
//        oldUrlThreadLocal.set(oldUrl);
//    }
//
//    public static Old getOldUrl() {
//        return (Old)oldUrlThreadLocal.get();
//    }

    public static void setUseDhfMan(Integer dhfMan) {
        useDhfManThreadLocal.set(dhfMan);
    }

    public static Integer getUseDhfMan() {
        return (Integer)useDhfManThreadLocal.get();
    }

    public static List<String> getHost() {
        Object hostList = new ArrayList();

        try {
            hostList = (List) TestListener.threadHost.get();
        } catch (NullPointerException var2) {
            log.error("请继承基础类 : BaseRequest -- threadHost 初始化失败");
            System.exit(0);
        }

        return (List)hostList;
    }

    public static void setHost(String newValue) {
        List<String> newList = getHost();
        newList.add(newValue);
        TestListener.threadHost.set(newList);
    }

    public static void setVariableData(final String newKey, final Object newValue) {
        Map<String, Object> newMap = getVariableData();
        newMap.putAll(new HashMap<String, Object>() {
            {
                this.put(newKey, newValue);
            }
        });
        TestListener.threadVariableData.set(newMap);
    }

    public static Map<String, Object> getVariableData() {
        Object variableData = new HashMap();

        try {
            variableData = (Map)TestListener.threadVariableData.get();
        } catch (NullPointerException var2) {
            log.error("TestListener -- threadVariableData  初始化失败");
            System.exit(0);
        }

        return (Map)variableData;
    }

    public static void setFunctionId(String key, String newValue) {
        Map<String, Set<String>> functionId = getFunctionId();
        Set<String> newSet = (Set)functionId.get(key);
        if (null == newSet) {
            newSet = new HashSet();
        }

        ((Set)newSet).add(newValue);
        functionId.put(key, newSet);
        TestListener.threadFunctionIdMap.set(functionId);
    }

    public static Map<String, Set<String>> getFunctionId() {
        Object functionId = new HashMap();

        try {
            functionId = (Map)TestListener.threadFunctionIdMap.get();
        } catch (NullPointerException var2) {
            log.error("TestListener -- threadFunctionIdSet  初始化失败");
            System.exit(0);
        }

        return (Map)functionId;
    }

    static void setSceneResultContentList(ResultContent resultContent) {
        List<ResultContent> newList = getSceneResultContentList();
        newList.add(resultContent);
        TestListener.threadScenelResultContentList.set(newList);
    }

    private static List<ResultContent> getSignalResultContentList() {
        Object newList = new ArrayList();

        try {
            newList = (List)TestListener.threadSignalResultContentList.get();
        } catch (NullPointerException var2) {
            log.error("TestListener -- threadSignalResultContentList  初始化失败");
            System.exit(0);
        }

        return (List)newList;
    }

    static void setSignalResultContentList(ResultContent resultContent) {
        List<ResultContent> newList = getSignalResultContentList();
        newList.add(resultContent);
        TestListener.threadSignalResultContentList.set(newList);
    }

    private static List<ResultContent> getSceneResultContentList() {
        Object newList = new ArrayList();

        try {
            newList = (List)TestListener.threadScenelResultContentList.get();
        } catch (NullPointerException var2) {
            log.error("TestListener -- threadScenelResultContentList  初始化失败");
            System.exit(0);
        }

        return (List)newList;
    }

//    public static void setGenericService(final String newKey, final GenericService newValue) {
//        ConcurrentHashMap<String, GenericService> newMap = getGenericService();
//        newMap.putAll(new ConcurrentHashMap<String, GenericService>() {
//            {
//                this.put(newKey, newValue);
//            }
//        });
//        TestListener.threadGenericService.set(newMap);
//    }
//
//    public static GenericService getGenericService(String key) {
//        ConcurrentHashMap genericService = new ConcurrentHashMap();
//
//        try {
//            genericService = (ConcurrentHashMap)TestListener.threadGenericService.get();
//        } catch (NullPointerException var3) {
//            log.error("TestListener -- threadGenericService  初始化失败");
//            System.exit(0);
//        }
//
//        return (GenericService)genericService.get(key);
//    }

//    private static ConcurrentHashMap<String, GenericService> getGenericService() {
//        ConcurrentHashMap genericService = new ConcurrentHashMap();
//
//        try {
//            genericService = (ConcurrentHashMap)TestListener.threadGenericService.get();
//        } catch (NullPointerException var2) {
//            log.error("TestListener -- threadGenericService  初始化失败");
//            System.exit(0);
//        }
//
//        return genericService;
//    }

    static boolean getTestSaveFlag() {
        return (Boolean)TestListener.threadSaveFlag.get();
    }
}
